package leetcode;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class LeetCode71 {
    public static void main(String[] args) {
        LeetCode71 leetCode71 = new LeetCode71();
        System.out.println(leetCode71.simplifyPath("/home/"));
    }

    public String simplifyPath(String path) {
        StringBuilder sb = new StringBuilder();
        ArrayDeque<String> queue = new ArrayDeque<>();
        String[] split = path.split("/");
        for (String s : split) {
            if (s.equals("..")) {
                if (queue.size() != 0)
                    queue.removeLast();
            } else if (!s.equals("") && !s.equals("."))
                queue.addLast(s);
        }
        if (queue.size() == 0)
            return "/";
        for (String s : queue)
            sb.append("/").append(s);
        return sb.toString();
    }
//------------------------------------------------------------------
}
